/** * VocabularyTrainer Copyright (C) 2015 André Schepers andreschepers81@gmail.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package eu.vocabularyexercise.domain; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine.Info; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; import javax.sound.sampled.UnsupportedAudioFileException; import static javax.sound.sampled.AudioSystem.getAudioInputStream; import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED; public class AudioFilePlayer { public static void main(String[] args) { final AudioFilePlayer player = new AudioFilePlayer(); player.playAudioFile("http://www.andreschepers.org/apple.mp3"); } public void playAudioFile(String urlString) { URL url; try { url = new URL(urlString); } catch (MalformedURLException ex) { ex.printStackTrace(); return; } try { AudioInputStream audioIn = AudioSystem.getAudioInputStream(url); } catch (UnsupportedAudioFileException ex) { Logger.getLogger(AudioFilePlayer.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(AudioFilePlayer.class.getName()).log(Level.SEVERE, null, ex); } SourceDataLine line = null; try (AudioInputStream ais = getAudioInputStream(url)) { AudioFormat audioFormat = getAudioFormat(ais.getFormat()); Info info = new Info(SourceDataLine.class, audioFormat); line = (SourceDataLine) AudioSystem.getLine(info); if (line != null) { line.open(audioFormat); line.start(); stream(getAudioInputStream(audioFormat, ais), line); line.drain(); line.stop(); line.close(); } } catch (IOException ex) { ex.printStackTrace(); } catch (UnsupportedAudioFileException ex) { ex.printStackTrace(); } catch (LineUnavailableException ex) { ex.printStackTrace(); } finally { if (line != null) line.close(); } } private AudioFormat getAudioFormat(AudioFormat audioFormat) { final int ch = audioFormat.getChannels(); final float rate = audioFormat.getSampleRate(); return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false); } private void stream(AudioInputStream in, SourceDataLine line) throws IOException { final byte[] buffer = new byte[65536]; for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) { line.write(buffer, 0, n); } } }